package com.api.projectY.controller;

import com.alibaba.fastjson2.JSONObject;
import com.api.common.core.controller.BaseController;
import com.api.common.core.domain.AjaxResult;
import com.api.common.utils.MessageUtils;
import com.api.core.domain.vo.tracing.intergration.IntergrationVo;
import com.api.core.domain.vo.tree.TreeVo;
import com.api.core.service.IIntergrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/intergration")
public class IntergrationController extends BaseController {

    @Autowired
    IIntergrationService iIntergrationService;

    @PostMapping("/generates")
    public AjaxResult generates(@RequestBody TreeVo vo) {
        return AjaxResult.success(iIntergrationService.generates(vo));
    }

    @PostMapping("/score")
    public AjaxResult scroe(@RequestBody IntergrationVo vo) {
        return AjaxResult.success(iIntergrationService.scroe(vo));
    }

    @PostMapping("/series")
    public AjaxResult series(@RequestBody IntergrationVo vo) {
        return AjaxResult.success(iIntergrationService.series(vo));
    }


    @PostMapping("/score/latest")
    public AjaxResult scoreLast(@RequestHeader("App-Language") String appLang,
                                @RequestBody JSONObject jo) {
        if (jo.containsKey("monitoredCodeList")) {
            List<String> monitoredCodeList = jo.getList("monitoredCodeList",String.class);
            return AjaxResult.success(iIntergrationService.scoreLast(monitoredCodeList));
        } else {
            return AjaxResult.error(MessageUtils.message(appLang, "common.param.null"));
        }
    }

    @PostMapping("/score/robot")
    public AjaxResult scoreRobot(@RequestBody JSONObject jo) {
        if (jo.containsKey("robotCodeList")) {
            List<String> robotCodeList = jo.getList("robotCodeList", String.class);
            return AjaxResult.success(iIntergrationService.scoreRobotList(robotCodeList));
        } else if (jo.containsKey("robotIdList")) {
            List<Long> robotIdList = jo.getList("robotIdList", Long.class);
            return AjaxResult.success(iIntergrationService.scoreRobotIds(robotIdList));
        } else {
            return AjaxResult.error("不存在对应参数");
        }
    }

    ;
}
